
package demo;
class Circle implements IGetArea{

    private double r;//半径

    public double getR() {
        return r;
    }

    public Circle(double r){
        this.r=r;
    }
    @Override
    public  double getArea(){
        return Math.PI*r*r;
    }
}
class Rect implements IGetArea{
    private double length;
    private double width;


    public Rect(double length, double width) {
        this.length = length;
        this.width = width;
    }
    public double getLength() {
        return length;
    }

    public double getWidth() {
        return width;
    }

    @Override
    public  double getArea(){
        return length*width ;
    }

}

public class Test {
    public static void method(IGetArea area){
        if(area instanceof Circle){
            Circle circle=(Circle) area;//向下转型
            System.out.println("圆的面积"+circle.getArea());
        }else if(area instanceof Rect){
            Rect rect=(Rect)area;//向下转型
            rect.getArea();
            System.out.println("矩形面积"+rect.getArea());
        }
    }

    public static void main(String[] args) {
        Circle circle=new Circle(5.7);
        Rect rect=new Rect(2.1,3.2);
        method(circle);
        method(rect);

    }




}
